/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.jiujiudevice.entity.JiuCompany;
import com.boxin.ims.modules.jiujiudevice.service.JiuCompanyService;
import com.google.common.collect.Lists;

/**
 * 设备公司Controller
 * @author sxh
 * @version 2018-08-15
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/jiujiudevice/jiuCompany")
public class JiuCompanyController extends BaseController {

	@Autowired
	private JiuCompanyService jiuCompanyService;
	
	@ModelAttribute
	public JiuCompany get(@RequestParam(required=false) Long id) {
		if (id != null){
			return jiuCompanyService.get(id);
		}else{
			return new JiuCompany();
		}
	}
	
	@RequiresPermissions("jiujiudevice:jiuCompany:view")
	@RequestMapping(value = {"list", ""})
	public String list(JiuCompany jiuCompany, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
        Page<JiuCompany> page = jiuCompanyService.find(new Page<JiuCompany>(request, response), jiuCompany); 
        model.addAttribute("page", page);
		return "modules/jiujiudevice/jiuCompanyList";
	}

	@RequiresPermissions("jiujiudevice:jiuCompany:view")
	@RequestMapping(value = "form")
	public String form(JiuCompany jiuCompany, Model model) {
	
		User user = UserUtils.getUser();
		model.addAttribute("jiuCompany", jiuCompany);
		return "modules/jiujiudevice/jiuCompanyForm";
	}

	@RequiresPermissions("jiujiudevice:jiuCompany:edit")
	@RequestMapping(value = "save")
	public String save(JiuCompany jiuCompany, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuCompany)){
			return form(jiuCompany, model);
		}
		jiuCompanyService.save(jiuCompany);
		addMessage(redirectAttributes, "保存设备公司'" + jiuCompany.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuCompany/?repage";
	}
	
	@RequiresPermissions("jiujiudevice:jiuCompany:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuCompanyService.delete(id);
		addMessage(redirectAttributes, "删除设备公司成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuCompany/?repage";
	}
	
   /**
    * 
    * @function: <p> 获得所有公司</p>
    * @param extId
    * @param request
    * @param response
    * @param model
    */
	@ResponseBody
	@RequestMapping(value = "treeData")
	public String treeData( HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<JiuCompany> cList =  jiuCompanyService.findAll();
		for(int i=0;i<cList.size();i++) {
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("pId", cList.get(i).getId());
			map.put("id", cList.get(i).getId());
			map.put("name", cList.get(i).getName());
			mapList.add(map);
		}
		return JsonMapper.getInstance().toJson(mapList);
	}
}

